<template>
	<view class="qdbox3">
		<!-- 	<view @click="back" class="backstyle">
			<u-icon name="arrow-left" size="40rpx" label="返回" color="#fff" labelColor="#fff" labelSize="36rpx">
			</u-icon>
		</view> -->
		<view v-if="vk.vuex.get('$app.appinfo.signin_image')">
			<view style="width: 100%; height: 250rpx;">
				<image @click="yq" style="width: 100%;height: 100%;border-radius: 30rpx;"
					:src="vk.vuex.get('$app.appinfo.signin_image')">
				</image>
			</view>
		</view>

		<siginIn :sign_list="sign_listed" :todaySign='todaySign' :sign_days="sign_days" @click="getsigin"></siginIn>

		<view style="background-color: #ffffff;">
			<view class="xsrw" style="margin-top: 60rpx;margin-bottom: 20px; padding: 20px;">
				新手任务
			</view>


			<view class="admin_box">
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<!-- <image class="avatar" src="@/static/yinghu.png"></image> -->
					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">解锁智能推荐</view>
							<image src="/pages_sub/static/jindou.jpg"
								style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+50金豆</view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">获取您的用户信息，解锁智能推荐</view>
					</view>
				</view>
				<view v-if="userInfo.finish.jiesuo" class="recharge" style="background-color: #bcbcbc;">
					<u-button size="mini" color="#bcbcbc" shape="circle" icon="已完成" iconColor="#fff"></u-button>
				</view>
				<view v-else class="recharge" @click="jiesuo">
					<u-button size="mini" color="#e74202" shape="circle" icon="去完成" iconColor="#fff"></u-button>
				</view>
			</view>


			<view class="admin_box">
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">新人签到</view>
							<image src="/pages_sub/static/jindou.jpg"
								style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+{{qiandao}}金豆</view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">每日签到领金豆</view>
					</view>
				</view>
				<view v-if="todaySign" class="recharge" style="background-color: #bcbcbc;">
					<u-button size="mini" color="#bcbcbc" shape="circle" icon="已完成" iconColor="#fff"></u-button>
				</view>
				<view v-else class="recharge">
					<u-button size="mini" color="#e74202" shape="circle" icon="待签到" iconColor="#fff"></u-button>
				</view>
			</view>


			<!-- 			<view class="admin_box">
				<view style="display: flex;justify-content: space-between;align-items: center;">

					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">邀请好友</view>
							<image src="@/static/jindou.jpg" style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+{{vk.vuex.get('$app.appinfo.beannumber')}}金豆</view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">
							每邀请1个好友奖励{{vk.vuex.get('$app.appinfo.beannumber')}}金豆</view>
					</view>
				</view>
				<view class="recharge" @click="yaoqing">
					<u-button size="mini" color="#e74202" shape="circle" icon="去邀请" iconColor="#fff"></u-button>
				</view>
			</view> -->


		</view>

		<!-- 成长任务 -->

		<view style="background-color: #ffffff;">


			<view class="xsrw" style="margin-top: 60rpx;margin-bottom: 20px; padding: 20px;">
				成长任务
			</view>
			<view class="admin_box">
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<!-- 	<image class="avatar" src="@/static/libao.png"></image> -->
					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">首充大礼包</view>
							<image src="/pages_sub/static/jindou.jpg"
								style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+50金豆</view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">充一次送两次金豆</view>
					</view>
				</view>
				<view v-if="userInfo.finish.libao" class="recharge" style="background-color: #bcbcbc;">
					<u-button size="mini" color="#bcbcbc" shape="circle" icon="已完成" iconColor="#fff"></u-button>
				</view>
				<view v-else class="recharge" @click="libao">
					<u-button size="mini" color="#e74202" shape="circle" icon="去完成" iconColor="#fff"></u-button>
				</view>
			</view>


			<view class="admin_box">
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">订阅提醒</view>
							<image src="/pages_sub/static/jindou.jpg"
								style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+15金豆</view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">订阅我，新剧上新不迷路</view>
					</view>
				</view>
				<view v-if="userInfo.finish.dingyue" class="recharge" style="background-color: #bcbcbc;">
					<u-button size="mini" color="#bcbcbc" shape="circle" icon="已完成" iconColor="#fff"></u-button>
				</view>
				<view v-else class="recharge" @click="dingyue">
					<u-button size="mini" color="#e74202" shape="circle" icon="去完成" iconColor="#fff"></u-button>
				</view>
			</view>

			<view class="admin_box" v-if="vk.vuex.get('$app.appinfo.wxad_switch')">
				<view style="display: flex;justify-content: space-between;align-items: center;">

					<view>
						<view class="dou">
							<view style="font-weight: 600;font-size: 15px;">看广告领金豆</view>
							<image src="/pages_sub/static/jindou.jpg"
								style="width: 30rpx;height: 30rpx;margin-left: 10px;">
							</image>
							<view class="dou2">+100金豆</view>
							<view class="dou2"></view>
						</view>
						<view style="color:gray;font-size: 12px;margin-top: 15px;">限量赠送，先到先得</view>
					</view>
				</view>
				<view class="recharge" @click="take_wxad">
					<u-button size="mini" color="#e74202" shape="circle" icon="看广告" iconColor="#fff"></u-button>
				</view>
			</view>

		</view>

		<!-- 、、签到成功提示弹窗 -->

		<!-- 	<u-popup :show="qdshow" mode="center" @close="close" round="20">
			<view style="width: 600rpx;height: 300rpx;border-radius: 20px;" class="qdpopub">
				<view @click="qdshow= false" style="margin-left: 90%;">
					<u-icon size="42rpx" name="close" color="#ffffff"></u-icon>
				</view>
				<view style="color: #ffffff; margin-left: 30px;">
					签到成功
				</view>
				<text
					style="color: #ffffff;margin-top: 50px;font-size: 10px;margin-left: 30px;">你已获得{{sign_listed[sign_days-1].discount}}金币</text>
				<button open-type="share" class="fixed_tc">分享在领50金币</button>
			</view>
		</u-popup> -->

	</view>
</template>

<script>
	const db = uniCloud.database();
	const _ = db.command;
	import siginIn from '../../components/fenxiao-sign/sigin-in.vue';
	export default {
		components: {
			siginIn
		},
		data() {
			return {
				kefuurl: "",
				userInfo: vk.vuex.get('$user.userInfo'),
				all_get: 0,
				qiandao: 30,
				sessionKey: "",
				encryptedKey: "",
				todayStart: "",
				yesterdayStart: "",
				yesterdayEnd: "",
				// qdshow: false,
				sign_days: 0, //后台返回累计签到天数
				todaySign: false,
				sign_listed: [{
						discount: '+30',
						day: '1'
					},
					{
						discount: '+30',
						day: '2'
					},
					{
						discount: '+30',
						day: '3'
					},
					{
						discount: '+30',
						day: '4'
					},
					{
						discount: '+30',
						day: '5'
					},
					{
						discount: '+30',
						day: '6'
					},
					{
						discount: '+30',
						day: '7'
					}
				],
				arrli: [{
						id: '1',
						title: '新手任务',
						children: [{
								id: '1',
								title: '首充大礼包',
								bz: '充一次送两次金豆'
							},
							{
								id: '2',
								title: '解锁智能推荐',
								bz: '获取你的智能信息，解锁智能推荐'
							},
							{
								id: '3',
								title: '获取手机号',
								bz: '完善用户信息'
							},
							{
								id: '4',
								title: '订阅提醒',
								bz: '订阅我新剧上新不迷路'
							},
							{
								id: '5',
								title: '新人签到',
								bz: '每日签到领金币'
							},
							{
								id: '6',
								title: '邀请好友',
								bz: '每邀请1个好友奖励100金豆'
							}
						]
					}

				]


			}
		},
		async onShow() {
			this.qiandao = vk.vuex.get('$app.appinfo.signin_reward')
			this.sign_listed.forEach(item => {
				item.discount = this.qiandao
			});

			this.all_get = 0;
			this.kefuurl = vk.vuex.get('$app.appinfo.workwxurl');

			let uid = vk.vuex.get('$user.userInfo._id');
			//判断用户登录状态(token有效)
			if (uid && vk.checkToken()) {
				vk.userCenter.getCurrentUserInfo({
					success: (data) => {
						console.log("注册登录成功", vk.vuex.get('$user.userInfo'));
						this.userInfo = data.userInfo;
						this.yiwancheng()
					}
				});
			} else {
				console.log("执行了自定义登录函数");
				await uni.vk.myfn.ttlogin();
			}
		},
		onShareAppMessage(res) {
			console.log('分享', res);

			if (res.from === 'button') {
				console.log("触发分享");
				if (this.todaySign == true) {
					vk.callFunction({
						url: 'client/user/kh/claim_rewards.qdzfx',
						title: '请求中...',
						data: {

						},
						success: (data) => {

						}
					});
				} else {
					vk.callFunction({
						url: 'client/user/kh/claim_rewards.fx',
						title: '请求中...',
						data: {

						},
						success: (data) => {

						}
					});
				}
				// 来自页面内转发按钮
				console.log(res.target)
				// this.qdshow = false
				//带上个人邀请码转发
				return {
					title: '', // 默认为小程序名称
					path: '/pages/index/index?invite_code=' + vk.vuex.get('$user.userInfo.my_invite_code'),
					imageUrl: '', // 默认为当前页面的截图
					success: (res) => {
						// 转发成功
						console.log('转发成功:', res);
					},
					fail: function(res) {
						console.log('转发失败:', res);
					},
				}
			}
		},
		onLoad() {
			//获取云端时间，避免本地时间可修改的bug
			vk.callFunction({
				url: 'user/kh/getcurrenttime',
				title: '请求中...',
				data: {

				},
				success: (data) => {
					this.todayStart = data.todayStart
					this.yesterdayStart = data.yesterdayStart
					this.yesterdayEnd = data.yesterdayEnd
					this.loadData()
				}
			});
		},
		methods: {
			yq() {
				uni.navigateTo({
					url: '/pages_sub/signin/wxads'
				})
			},
			ding(backToIndex) {
				console.log("56456454545454545");
				let that = this;
				uni.requestSubscribeMessage({

					tmplIds: vk.vuex.get('$app.msglist'),
					success(res) {
						if (that.show_redicon) {
							uni.showToast({
								title: "操作成功"
							})
						} else {
							uni.showToast({
								title: "添加追剧成功"
							})
						}
						vk.callFunction({
							url: 'client/user/kh/dingyue',
							showLoading: false,
							data: {
								msglist: vk.vuex.get('$app.msglist'),
								tv_id: that.tvinfo._id,
								tv_name: that.tvinfo.tv_name,
							},
						})

						if (backToIndex) {
							uni.navigateBack()
						}
					}
				})
			},
			//招商daili
			zsdl() {
				if (this.userInfo.finish.zsdl == false) {
					vk.callFunction({
						url: 'client/user/kh/claim_rewards.zsdl',
						title: '请求中...',
						data: {

						},
						success: (data) => {

						}
					});
				}
				uni.navigateTo({
					url: "/pages_sub/qrcode/qrcode"
				})
			},
			//添加客服微信
			addwx() {
				if (this.userInfo.finish.tjkf == false) {
					vk.callFunction({
						url: 'client/user/kh/claim_rewards.kefu',
						title: '请求中...',
						data: {

						},
						success: (data) => {

						}
					});
				}

				uni.navigateTo({
					url: '/pages_sub/qrcode/qrcode'
				})

			},

			yiwancheng() {
				this.all_get = 0;
				if (this.userInfo.finish.dingyue) {
					this.all_get += 1
				}
				if (this.userInfo.finish.jiesuo) {
					this.all_get += 1
				}
				if (this.userInfo.finish.libao) {
					this.all_get += 1
				}
				if (this.userInfo.finish.shoujihao) {
					this.all_get += 1
				}
				if (this.userInfo.finish.xinren) {
					this.all_get += 1
				}
			},
			async libao() {
				console.log("libao");
				vk.callFunction({
					url: 'client/user/kh/taskCenter',
					title: '请求中...',
					data: {
						type: "libao"
					},
					success: (data) => {
						// console.log("data");
						if (data.recharge == 0) {
							vk.navigateTo("/pages_sub/recharge/recharge");
						} else {
							this.userInfo.finish.libao = true
							this.userInfo.balance += 50
							vk.toast('任务已完成', 'success');
						}
					}
				});
			},
			jiesuo() {
				console.log("jiesuo");
				//如果已授权
				if (vk.vuex.get('$user.userInfo.nickname')) {
					vk.callFunction({
						url: 'client/user/kh/taskCenter',
						title: '请求中...',
						data: {
							type: "jiesuo",
						},
						success: (data) => {
							this.userInfo.finish.jiesuo = true
							this.userInfo.balance = this.userInfo.balance + data.jiangli
							vk.toast('任务已完成', 'success');
						}
					});
				} else {
					uni.navigateTo({
						url: "/pages_sub/setUserinfo/setUserinfo"
					})
				}

			},

			dingyue() {
				let that = this;
				uni.requestSubscribeMessage({
					tmplIds: vk.vuex.get('$app.msglist'),
					success(res) {
						console.log("订阅成功");

						that.dingyue_cloud()
					}
				})
			},
			dingyue_cloud() {
				vk.callFunction({
					url: 'client/user/kh/dingyue',
					showLoading: false,
					data: {
						msglist: vk.vuex.get('$app.msglist'),
						platform: "toutiao",
						tv_id: "no",
						tv_name: "no"
					},
				})

				vk.callFunction({
					url: 'client/user/kh/taskCenter',
					title: '请求中...',
					data: {
						type: "dingyue"
					},
					success: (data) => {
						this.userInfo.finish.dingyue = true
						this.userInfo.balance = this.userInfo.balance + data.jiangli
						vk.toast('任务已完成', 'success');
					}
				});
			},
			xinren() {
				// uni.navigateTo({
				// 	url: "/pages_sub/signin/signin"
				// })

				// uni.navigateBack({
				// 	delta: 1
				// })
				//企业微信客服功能
				// wx.openCustomerServiceChat({
				// 	extInfo: {
				// 		url: this.kefuurl
				// 	},
				// 	corpId: 'wwc6b9dde9f3e07199',
				// 	success(res) {
				// 		调用客服成功
				// 	}
				// })
			},
			// yaoqing() {
			// 	uni.navigateTo({
			// 		url: "/pages/mypriress/mypriress"
			// 	})
			// },
			take_wxad() {
				uni.navigateTo({
					url: "/pages_sub/signin/wxads"
				})
			},

			getsigin() {
				this.signin()
			},
			async loadData() {
				//获取昨日签到数据
				let sign_datas = await db.collection('a_sign').where({
					user_id: vk.vuex.get('$user.userInfo._id'),
					_add_time: _.gte(this.yesterdayStart).lte(this.yesterdayEnd)
				}).get()
				console.log("sign_datas=", sign_datas);
				if (sign_datas.result.data.length != 0) {
					this.sign_days = sign_datas.result.data[0].sign_datas || 0;
				}
				let todatStart = vk.vuex.get('$app.time.todayStart');
				const check_sign = await db.collection('a_sign').where({
					user_id: vk.vuex.get('$user.userInfo._id'),
					_add_time: _.gte(this.todayStart)
				}).get()
				if (check_sign.result.data.length != 0) {
					if (!check_sign.result.data[0].sign_datas) {
						check_sign.result.data[0].sign_datas = 1
					}
					this.sign_days = this.sign_days + 1;
					this.todaySign = true
				}
				vk.hideLoading();

				// this.signin()
			},
			async signin() {
				//检查今日是否签到，签到则不显示签到提示框
				let todatStart = vk.vuex.get('$app.time.todayStart')
				const check_signCount = await db.collection('a_sign').where({
					user_id: vk.vuex.get('$user.userInfo._id'),
					_add_time: _.gte(this.todayStart)
				}).count()
				if (check_signCount.result.total > 0) {
					//修改本地数据
					this.todaySign = true
					vk.toast('今日已签到', 'success');
					vk.vuex.set('$user.userInfo.todaySign', true)
					return;
				}
				this.ding(true)
				vk.callFunction({
					url: 'client/user/kh/claim_rewards.lxqd',
					title: '请求中...',
					data: {
						type: "qiandao",
						signin_reward: vk.vuex.get('$app.appinfo.signin_reward') || 30, //未设置默认30
						sign_datas: this.sign_days //连续签到天数
					},
					success: (data) => {
						// vk.toast('签到完成', 'success');
						//修改本地数据
						this.todaySign = true
						// this.qdshow = true
						this.loadData()
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.qdswper {
		background-image: url(https://mp-337d4240-4b16-4862-8388-8c110c3a4ccb.cdn.bspapp.com/cloudstorage/ed981cdf-9fb4-4f7d-abb0-25de205b202c.png)
	}

	.fixed_tc {
		border-radius: 20px;
		color: orangered;
		width: 500rpx;
		height: 40px;
		// margin-top: 75rpx;
		position: absolute;
		top: 195rpx;
		left: 50rpx;
		line-height: 40px;
		font-size: 12px;
		background-color: yellow;


	}

	.dou2 {
		color: #ff5500;
		font-size: 26rpx;
		font-weight: 600;
	}

	.qdbox3 {
		background-color: #f5f6fa;
		width: 90%;
		height: 100%;
		padding: 20px;
		overflow-x: hidden;
	}

	.qdpopub {
		background-image: url(https://mp-337d4240-4b16-4862-8388-8c110c3a4ccb.cdn.bspapp.com/cloudstorage/a4ed5f79-3d3c-4edb-904f-77fd1673c15e.png);

		background-repeat: no-repeat;
		background-size: 100% 100%;
	}

	.u-safe-area-inset-bottom {
		padding-bottom: 0px
	}

	.xsrw {
		color: orange;
		font-size: 35rpx;
		font-weight: 600;
		height: 80rpx;
		line-height: 80rpx;
		border-bottom: 1px dashed orange;
	}

	.button-link {
		width: 150rpx;
		height: 60rpx;
		font-size: 12px;
		color: orange;
		border-radius: 30rpx;
		font-weight: 600;
		border: 1px solid orange;
		text-align: center;
		line-height: 60rpx;
	}


	.admin_box {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 20rpx;
		padding: 20rpx;
		margin-top: -20rpx;
		background-color: #fff;
		border-radius: 0 0 20rpx 20rpx;
	}

	.avatar {
		width: 60rpx;
		height: 60rpx;
		padding: 20rpx;
		border-radius: 10rpx;
		background-color: #ffe9e1;
	}

	.nickname {
		color: #bcbcbc;
		font-size: 24rpx;
		margin: 10rpx 0 0 20rpx;
	}

	.dou {
		display: flex;
		align-items: center;
	}

	.dou1 {
		color: #000000;
		font-weight: 600;
		font-size: 30rpx;
		margin: 0 20rpx 0 20rpx;
	}

	.dou2 {
		color: #ff5500;
		font-size: 26rpx;
		font-weight: 600;

	}

	.recharge {
		color: #fff;
		background-color: #e74202;
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		text-align: center;
	}
</style>